package com.wangkang.msm.service.impl;
import com.alibaba.fastjson.JSONObject;
import com.aliyuncs.CommonRequest;
import com.aliyuncs.CommonResponse;
import com.aliyuncs.DefaultAcsClient;
import com.aliyuncs.IAcsClient;
import com.aliyuncs.http.MethodType;
import com.aliyuncs.profile.DefaultProfile;
import com.wangkang.msm.constant.SmsConstant;
import com.wangkang.msm.enums.AliSmsParamEnum;
import com.wangkang.msm.service.MsmService;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Service;
import java.util.Map;

@Service
public class MsmServiceImpl implements MsmService {

    /**
     * 发送验证码
     *
     * @param param 验证码
     * @param phone 手机号
     * @return 发送结果
     */
    @Override
    public boolean send(Map<String, Object> param, String phone) {

//        keyId: LTAI4Fzy2CEM3GRmnoKmyWVH
//        keySecret: QqCAj7HZpsknuQIIWcIMX6ckyCW9HK


        if (StringUtils.isBlank(phone)){
            return false;
        }
        DefaultProfile profile =
                DefaultProfile.getProfile("cn-hangzhou", "LTAI4Fzy2CEM3GRmnoKmyWVH", "QqCAj7HZpsknuQIIWclMX6ckyCW9HK");
        IAcsClient client = new DefaultAcsClient(profile);

        //设置相关固定的参数
        CommonRequest request = new CommonRequest();
        //request.setProtocol(ProtocolType.HTTPS);
        request.setSysMethod(MethodType.POST);
        //request.setSysRegionId("cn-hangzhou");
        request.setSysDomain("dysmsapi.aliyuncs.com");
        request.setSysVersion("2017-05-25");
        request.setSysAction("SendSms");

        request.putQueryParameter(AliSmsParamEnum.REGIONID.getCode(), SmsConstant.ALISMS_REGIONID);
        //设置发送相关的参数
        //手机号
        request.putQueryParameter("PhoneNumbers",phone);
        //申请阿里云 签名名称
        request.putQueryParameter("SignName","智慧教培");
        //申请阿里云 模板code
        request.putQueryParameter("TemplateCode","SMS_186900698");
        //验证码数据，转换json数据传递
        request.putQueryParameter("TemplateParam", JSONObject.toJSONString(param));

        try {
            //最终发送
            CommonResponse response = client.getCommonResponse(request);
            boolean success = response.getHttpResponse().isSuccess();
            return success;
        }catch(Exception e) {
            e.printStackTrace();
            return false;
        }

    }
}